<?php

/**
 *
 * Detection for mobile device and redirecting
 * user to WAP versinon
 *
 * @author Tomas Kopecny <tomas@kopecny.info>
 * @version 0.1
 * @copyright 2007 - 2008 Tomas Kopecny
 * @url http://code.google.com/p/detectmobile/
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 *     02111-1307, USA.
 *
 * $Id: dm.php 10 2009-06-10 15:49:19Z georgo10 $
 *
 * Changelog:
 *             2009-06-10 - Added detection for Opera Mobile 9.x (HTC branded)
 *             2008-07-17 - Added detection for Opera Mobile 9.5 beta
 *             2008-07-16 - Added UA for Google Mobile Wrapper
 */


/**
 * Detect mobile device
 *
 * @description If is mobile device detected, cookie set for performance for next detecting
 *
 * @param void
 * @return bool information about detected mobile device
 */
function detect_mobile_device()
{

	// Provest detekci mobilniho zarizeni
	$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
	$mobile_agents = array('acs-','alav','alca','amoi','audi','aste','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','g10-', 'hipt','htc ','htc_','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','opwv','palm','pana','pant','pdxg','phil','play','pluc','port','prox','qtek','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
	if (
		($_COOKIE['ismobile']=='1') ||
		(strpos(strtolower($_SERVER['HTTP_ACCEPT']),'text/vnd.wap.wml')!==false) || 
		(strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')!==false) || 
		(isset($_SERVER['HTTP_X_WAP_PROFILE'])) || 
		(isset($_SERVER['HTTP_PROFILE'])) || 
		(isset($_SERVER['X-OperaMini-Features'])) || 
		(isset($_SERVER['UA-pixels'])) ||
		(in_array($mobile_ua,$mobile_agents)) ||
		(preg_match('/(up.browser|up.link|windows ce|google wireless transcoder|iemobile|mmp|symbian|smartphone|midp|wap|phone|vodafone|pocket|mobile|pda|psp|ppc|mobi)/i',strtolower($_SERVER['HTTP_USER_AGENT'])))
	) {
		// Detected mobile device

		if(!headers_sent()) // For better performance on next detection
		{
			setcookie('ismobile', 1, time()+3600*24*31, '/', '.yourdomain.com');
		}

		return TRUE;
	}
	return FALSE;
};

?>
